#coding=utf-8
'''
# @Name:__init__.py
# @author:mysouil
# @Description:init操作
# @DATE:2022/9/21 0:58
# @Modified By:
'''
from flask import Flask
from settings.default import map_config
from comment.models import db
from shopping.resources.address import address_bp
from shopping.resources.cart import cart_bp
from shopping.resources.coupon import coupon_bp
from shopping.resources.goods import goods_bp
from shopping.resources.order import order_bp
from shopping.resources.user import user_bp
from shopping.resources.index import index_bp
from comment.utils.log import create_logger
from comment.utils.limit import limiter
from comment.utils.shopping_redis import redis_client
from comment.utils.request_hook import jwt_request_auth

# 创建app
def creat_app(config_type):
    app = Flask(__name__)
    # 加载项目的配置
    app.config.from_object(map_config.get(config_type))
    
    # 初始化限流器
    limiter.init_app(app)
    
    # 加载日志处理工具
    create_logger(app)
    
    # 初始化sqlalchemy
    db.init_app(app)
    
    # 初始化redis数据库连接
    redis_client.init_app(app)
    
    # 添加请求钩子(全局)
    app.before_request(jwt_request_auth)  # 所有请求都生效
    
    # 加载模块的蓝图
    app.register_blueprint(user_bp)
    app.register_blueprint(index_bp)
    app.register_blueprint(goods_bp)
    app.register_blueprint(coupon_bp)
    app.register_blueprint(cart_bp)
    app.register_blueprint(address_bp)
    app.register_blueprint(order_bp)
    
    return app
    